home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / etc / graph / read_data.cc < prev    next >
C/C++ Source or Header  |  1993-01-19  |  3KB  |  115 lines

  1. #include "read_data.h"
  2.  
  3. // READ_DATA reads ascii (or binary) data from an istream of
  4. // coordinates and labels.  It passes back the array of points (x, y,
  5. // and label) containing the data.
  6.      
  7. void
  8. read_data (istream& in, char* in_filename, pointXPlex &pplex,
  9.        int auto_abscissa, double x_start,
  10.        double delta_x, int &symbol_number,
  11.        data_type input_data, int switch_symbols)
  12. {
  13.   char next_char;        // next character to be read
  14.   point p;            // the point we are currently reading in.
  15.   p.label = (char *) 0;        // binary files contain no labels.
  16.   double prev_x = -DBL_MAX;    // the previous x value.
  17.   
  18.   if (auto_abscissa)
  19.     x_start -= delta_x;
  20.   
  21.   if (input_data == ASCII)
  22.     {                // input contains ascii data
  23.       while (in.good ())
  24.     {
  25.       if (auto_abscissa)
  26.         p.x = x_start = x_start + delta_x;
  27.       else
  28.         in >> p.x >> WS;
  29.       if (switch_symbols && (p.x < prev_x))
  30.         symbol_number++;
  31.       prev_x = p.x;
  32.       
  33.       in >> p.y;
  34.       if (!in.good ())        // if we read x but not y complain.
  35.         {
  36.           char *input_line;
  37.           in.clear ();
  38.           in.gets (&input_line);
  39.           // if the input contains one coordinate per line
  40.           // you win here --- emacs can find the source
  41.           // line from this error message.
  42.           cerr << in_filename << ":" << 2 + pplex.high()
  43.         << ": unable to read the" sp 2 + pplex.high()
  44.           << ((2 + pplex.high() == 1) ? "st" : "th")
  45.           << " y coordiate.\n";
  46.           if (input_line)
  47.         if (strlen (input_line))
  48.             cerr << in_filename << ":" << 2 + pplex.high()
  49.             << ": unexpected `" << input_line << "'\n";
  50.           break;
  51.         }
  52.       in >> WS;            // skip white space after y coordinate
  53.       p.label = (char *) 0;            // by default there is no label
  54.       in.get (next_char);                // look ahead for a label
  55.       if (in.good ())
  56.         {
  57.           in.putback (next_char);
  58.           if (!isdigit (next_char)    // if a lable is found
  59.           && (next_char != '.')
  60.           && (next_char != '+')
  61.           && (next_char != '-'))
  62.         {
  63.           in.gets (&p.label);        // store it with x and y
  64.         }
  65.           in >> WS;            // skip white space after label
  66.         }
  67.       p.symbol = symbol_number;
  68.       pplex.add_high (p);
  69.     }
  70.       return;
  71.     }
  72.  
  73.   if (input_data == DOUBLE)
  74.     {                // input contains binary double precision
  75.       while (in.good ())
  76.     {
  77.       if (auto_abscissa)
  78.         p.x = x_start = x_start + delta_x;
  79.       else
  80.         {
  81.           in.read (&p.x, sizeof(p.x));
  82.           if (switch_symbols && (p.x < prev_x))
  83.         symbol_number++;
  84.           prev_x = p.x;
  85.         }
  86.       in.read (&p.y, sizeof(p.y));
  87.       p.symbol = symbol_number;
  88.       if (in.good ())
  89.         pplex.add_high (p);
  90.     }
  91.     }
  92.   if (input_data == INT)
  93.     {                // input contains binary integers
  94.       int i;
  95.       while (in.good ())
  96.     {
  97.       if (auto_abscissa)
  98.         p.x = x_start = x_start + delta_x;
  99.       else
  100.         {
  101.           in.read (&i, sizeof(i));
  102.           p.x = i;
  103.           if (switch_symbols && (p.x < prev_x))
  104.         symbol_number++;
  105.           prev_x = p.x;
  106.         }
  107.       in.read (&p.y, sizeof(p.y));
  108.       p.symbol = symbol_number;
  109.       if (in.good ())
  110.         pplex.add_high (p);
  111.     }
  112.     }
  113.   return;
  114. }
  115.